package spring.boot.wx.mini.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 接口返回通用封装
 */
@ApiModel("通用接口返回对象")
public class Results<T> {
    /**
     * 成功code
     */
    public static final int SUCCESS_CODE = 0;

    /**
     * 参数错误 code
     */
    private static final int PARAM_ERROR_CODE = 100;

    /**
     * token错误 code
     */
    private static final int TOKEN_ERROR_CODE = 405;

    /**
     * 未知错误 code
     */
    private static final int UNKNOWN_ERROR_CODE = 500;


    /**
     * 成功 msg
     */
    public static final String SUCCESS_MSG = "SUCCESS";

    /**
     * 未知错误 msg
     */
    private static final String UNKNOWN_ERROR = "UNKNOWN_ERROR";


    @ApiModelProperty(required = true, notes = "结果码", example = "0")
    private int code;
    @ApiModelProperty(required = true, notes = "时间戳", example = "1567425139000")
    private long time;
    @ApiModelProperty(required = true, notes = "返回信息", example = "SUCCESS")
    private String msg;
    @ApiModelProperty(required = true, notes = "返回数据")
    private T content;

    public Results() {
    }


    public Results(int code, String msg, T content) {
        this.code = code;
        this.msg = msg;
        this.content = content;
        this.time = System.currentTimeMillis();
    }

    /**
     * 默认成功结果
     */
    public static Results success() {
        Results results = new Results();
        results.setCode(SUCCESS_CODE);
        results.setMsg(SUCCESS_MSG);
        results.setTime(System.currentTimeMillis());
        return results;
    }

    /**
     * 默认成功结果
     */
    public static Results success(Object content) {
        Results results = new Results();
        results.setCode(SUCCESS_CODE);
        results.setMsg(SUCCESS_MSG);
        results.setContent(content);
        results.setTime(System.currentTimeMillis());
        return results;
    }

    /**
     * 默认未知错误结果
     */
    public static Results error() {
        Results results = new Results();
        results.setCode(UNKNOWN_ERROR_CODE);
        results.setMsg(UNKNOWN_ERROR);
        results.time = System.currentTimeMillis();
        return results;
    }

    /**
     * 未知错误结果  自定义消息
     */
    public static Results error(String message) {
        Results results = new Results();
        results.setCode(UNKNOWN_ERROR_CODE);
        results.setMsg(message);
        results.time = System.currentTimeMillis();
        return results;
    }


    /**
     * 参数错误结果
     */
    public static Results paramError(String msg) {
        Results results = new Results();
        results.setCode(PARAM_ERROR_CODE);
        results.setMsg(msg);
        results.time = System.currentTimeMillis();
        return results;
    }

    /**
     * token错误结果
     */
    public static Results tokenError(String msg) {
        Results results = new Results();
        results.setCode(TOKEN_ERROR_CODE);
        results.setMsg(msg);
        results.time = System.currentTimeMillis();
        return results;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }
}
